package de.itagile.hypersphere

import de.itagile.hypersphere.EjbJarXmlParser
import de.itagile.hypersphere.mock.MapxmiAdapterStub
class EjbTemplateProcessesEntityWithRelationshipTest extends GroovyTestCase {
	def xml = '''
		<ejb-jar id="ejb-jar_ID" version="2.1">
			<enterprise-beans>
				<entity id="Book">
					<ejb-name>Book</ejb-name>
					<local>ejbs.BookLocal</local>
					<prim-key-class>java.lang.Integer</prim-key-class>
					<primkey-field>id</primkey-field>
				</entity>
				<entity id="Page">
					<ejb-name>Page</ejb-name>
					<local>test.PageLocal</local>
					<prim-key-class>java.lang.Integer</prim-key-class>
					<primkey-field>id</primkey-field>
				</entity>
			</enterprise-beans>
			<relationships>
				<ejb-relation id="1">
					<ejb-relation-name>Page-Book</ejb-relation-name>
					<ejb-relationship-role>
						<ejb-relationship-role-name>
							book
						</ejb-relationship-role-name>
						<multiplicity>Many</multiplicity>
						<relationship-role-source>
							<ejb-name>Page</ejb-name>
						</relationship-role-source>
						<cmr-field>
							<cmr-field-name>book</cmr-field-name>
						</cmr-field>
					</ejb-relationship-role>
					<ejb-relationship-role>
						<ejb-relationship-role-name>
							page
						</ejb-relationship-role-name>
						<multiplicity>One</multiplicity>
						<relationship-role-source>
							<ejb-name>Book</ejb-name>
						</relationship-role-source>
						<cmr-field>
							<cmr-field-name>page</cmr-field-name>
							<cmr-field-type>
								java.util.Collection
							</cmr-field-type>
						</cmr-field>
					</ejb-relationship-role>
				</ejb-relation>
			</relationships>
		</ejb-jar>
			'''
			
	def ejbJar = new EjbJarXmlParser().parseText(xml)
	def book = ejbJar.enterprisebeans.entity[0]
	def page = ejbJar.enterprisebeans.entity[1]
	def bookEntityTemplate = new EjbTemplate(ejbJar,
			new MapxmiAdapterStub(),
			new EntityTemplateFactory())
				.getEntityTemplate(book)
	def pageEntityTemplate = new EjbTemplate(ejbJar,
			new MapxmiAdapterStub(),
			new EntityTemplateFactory())
				.getEntityTemplate(page)
	
	void testSetOppositeFqnOnEntityTemplate() {
		assertEquals("test.Page", bookEntityTemplate.relationships[0].fqn)
		assertEquals("ejbs.Book", pageEntityTemplate.relationships[0].fqn)
	}
		
	void testSetTypeOnEntityTemplate() {
		assertEquals("ManyToOne", bookEntityTemplate.relationships[0].type)
		assertEquals("OneToMany", pageEntityTemplate.relationships[0].type)
	}
	
	void testSetCollectionOnEntityTemplate() {
		assertEquals("java.util.Collection", bookEntityTemplate.relationships[0].collection)
		assertEquals("", pageEntityTemplate.relationships[0].collection)
	}
		
}